<?php
namespace Admin\Controller;
use Admin\Model\goodsModel;
use Admin\Model\memberModel;
use Admin\Model\orderModel;
use Admin\Model\withdrawModel;

class IndexController extends BaseController {
    
    
    public $goodsModel;
    public $memberModel;
    public $orderModel;
    public $withdrawModel;

    public function __construct() {
        parent::__construct();
        $this->goodsModel = new goodsModel();
        $this->memberModel = new memberModel();
        $this->orderModel = new orderModel();
        $this->withdrawModel = new withdrawModel();
    }
    
    /*
     * 获取订单总额
     */
    protected function _getOrderMoney($time1,$time2){
        //昨日订单总额
        $order_where = array(
            "checked" => array("IN",array(1,2,5)),//0待支付,1已付款,,2完成,3.待退款 4已退款,5发货,0未完成,6订单关闭,
        );

        $order_where["pay_time"] = array("between",array($time1,$time2));//依据付款时间
        
        $a = $this->orderModel->where($order_where)->sum("cash_money");
        $b = $this->orderModel->where($order_where)->sum("pay_money");
		
	//	echo $this->orderModel->getLastSql();
        $c = $this->orderModel->where($order_where)->sum("yongjin_money");
        $d = $this->orderModel->where($order_where)->sum("prize_money");
        $order_money = round($a+$b+$c+$d,2);
        return $order_money ? $order_money : 0;
        
    }

    /*
     * 订单统计
     */
    protected function _getOrderCount($time1,$time2,$checked){
        //昨日订单总额
        $order_where = array(
            "checked" => $checked,//0待支付,1已付款,,2完成,3.待退款 4已退款,5发货,0未完成,6订单关闭,
        );
        
        if(in_array($checked,array(1,2,5))){
            $order_where["pay_time"] = array("between",array($time1,$time2));//依据付款时间
        }else{
            
        }
        
    
        $a = $this->orderModel->getOrderCount($order_where);
        return $a;
    
    }
    
    /*
     * 成交订单数量
     */
    protected function _getOrderC($time1,$time2){
        $order_where = array(
            "checked" => array("IN","1,2,5"),//0待支付,1已付款,,2完成,3.待退款 4已退款,5发货,0未完成,6订单关闭,
        );
        
        $order_where["pay_time"] = array("between",array($time1,$time2));//依据付款时间
        
        $a = $this->orderModel->getOrderCount($order_where);
        return $a;
    }
    
    

    /*
     * 获取订单总额
     */
    protected function _getOrderMoney2($time1,$time2){
        //昨日订单总额
        $order_where = array(
            "checked" => array("IN",array(1,2,5)),//0待支付,1已付款,,2完成,3.待退款 4已退款,5发货,0未完成,6订单关闭,
            "goods_ids" => 4,
        );

        $order_where["pay_time"] = array("between",array($time1,$time2));//依据付款时间
        
        $a = $this->orderModel->where($order_where)->sum("cash_money");
        $b = $this->orderModel->where($order_where)->sum("pay_money");
        $c = $this->orderModel->where($order_where)->sum("yongjin_money");
        $d = $this->orderModel->where($order_where)->sum("prize_money");
        $order_money = round($a+$b+$c+$d,2);
        return $order_money ? $order_money : 0;
        
    }
    
    /*
     * 成交订单数量
     */
    protected function _getOrderC2($time1,$time2){
        $order_where = array(
            "checked" => array("IN","1,2,5"),//0待支付,1已付款,,2完成,3.待退款 4已退款,5发货,0未完成,6订单关闭,
            "goods_ids" => 4
        );
    
        $order_where["pay_time"] = array("between",array($time1,$time2));//依据付款时间
    
        $a = $this->orderModel->getOrderCount($order_where);
        return $a;
    }
    
    
    
    
    /*首页*/
    public function index(){
        //昨天
        $time1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))-24*60*60);//昨天开始
        $time2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))-1);//昨天最后一秒

        //今天
        $today1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d")));
        $today2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+24*60*60-1);

        //本周
        $week1 = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-date("w")-6,date("Y")));
        $week2 = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w"),date("Y")));
        
        //上周
        $week3 = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-date("w")-13,date("Y")));
        $week4 = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")-7,date("Y")));
        //本月
        $month1 = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),1,date("Y")));
        $month2 = date("Y-m-d H:i:s",strtotime(date('Y-m-01', strtotime(date("Y-m-d").' +1 month -1 day')))-1);
        
        //上月
        $month3 = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")));
        $month4 = date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y")));
        
        
        //库存少于10件统计
        $where_g = array(
            "stock" => array("lt",10),
            "ischeck" =>1
        );
        $g_count = $this->goodsModel->getGoodsCount($where_g);
        $data = array(
            "g_count" => $g_count,
            
        );
        
        //用户总数
        $mall = $this->memberModel->getMemberCount();
        //已购买的用户总数
        $m_buy_where = array(
            "grade_id" => array(
                "gt",1
            )
        );
        $m_buy = $this->memberModel->getMemberCount($m_buy_where);
        $data['m_buy'] = $m_buy;//分销商总数
        $data['mall'] = $mall;//用户总数
        
        $data['order_yesterday'] = $this->_getOrderMoney($time1,$time2);//昨天的订单金额
        $data['order_yesterday_num'] = $this->_getOrderC($time1,$time2);//昨天的订单数
        
        //今天订单总额
        $data['order_today'] = $this->_getOrderMoney($today1,$today2);//今天的订单金额
        $data['order_today_num'] = $this->_getOrderC($today1,$today2);//今天的订单数
        
        //本周金额
        $data['order_week'] = $this->_getOrderMoney($week1,$week2);//本周的订单金额
        $data['order_week_num'] = $this->_getOrderC($week1,$week2);//本周的订单数
        
        //上周
        $data['order_lastweek'] = $this->_getOrderMoney($week3,$week4);//上周的订单金额
        $data['order_lastweek_num'] = $this->_getOrderC($week3,$week4);//上周的订单数
        
        //本月
        $data['order_month'] = $this->_getOrderMoney($month1,$month2);//本月的订单金额
        $data['order_month_num'] = $this->_getOrderC($month1,$month2);//本月的订单数
        
        //下月
        $data['order_lastmonth'] = $this->_getOrderMoney($month3,$month4);//上月的订单金额
        $data['order_lastmonth_num'] = $this->_getOrderC($month3,$month4);//上月的订单数
        
        //总的
        $data['order_all'] = $this->_getOrderMoney('2015-12-01 00:00:00',$today2);//所有的订单金额
        $data['order_all_num'] = $this->_getOrderC('2015-12-01 00:00:00',$today2);//所有的的订单数
        
        
        //0待支付,1已付款,,2完成,3.待退款 4已退款,5发货,0未完成,6订单关闭,
        $data['order_daifahuo'] = $this->_getOrderCount('2015-12-01 00:00:00',$today2,1);//待发货订单
        
        $data['order_tuihuo'] = $this->_getOrderCount('2015-12-01 00:00:00',$today2,3);//待退款订单
        
        $data['order_nopay'] = $this->_getOrderCount('2015-12-01 00:00:00',$today2,0);//待付款订单
        
        //已经支出的总佣金
        $where_w1 = array(
            "status" => array("IN","1")
        );
        $withdraw_moneyed = $this->withdrawModel->where($where_w1)->sum("atm_money");
        $data['withdraw_moneyed'] = $withdraw_moneyed ? $withdraw_moneyed : 0;
        
        
        $where_w2 = array(
            "status" => array("IN","0")
        );
        $withdraw_moneyimg = $this->withdrawModel->where($where_w2)->sum("atm_money");
        $data['withdraw_moneyimg'] = $withdraw_moneyimg ? $withdraw_moneyimg : 0;
        $this->assign('data',$data);
        $this->display("cangku");
    }
    
    public function cangku () {
        //昨天
        $time1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))-24*60*60);//昨天开始
        $time2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))-1);//昨天最后一秒

        //今天
        $today1 = date("Y-m-d H:i:s",strtotime(date("Y-m-d")));
        $today2 = date("Y-m-d H:i:s",strtotime(date("Y-m-d"))+24*60*60-1);

        //本周
        $week1 = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-date("w")-6,date("Y")));
        $week2 = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w"),date("Y")));
        
        //上周
        $week3 = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date('d')-date('w')+1-7,date("Y")));
        $week4 = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date('d')-date('w')+7-7,date("Y")));
        //本月
        $month1 = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),1,date("Y")));
        $month2 = date("Y-m-d H:i:s",strtotime(date('Y-m-01', strtotime(date("Y-m-d").' +1 month -1 day')))-1);
        
        //上月
        $month3 = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")));
        $month4 = date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y")));
        
        
        //库存少于10件统计
        $where_g = array(
            "stock" => array("lt",10),
            "ischeck" =>1
        );
        $g_count = $this->goodsModel->getGoodsCount($where_g);
        $data = array(
            "g_count" => $g_count,
            
        );
        
        //用户总数
        $mall = $this->memberModel->getMemberCount();
        //已购买的用户总数
        $m_buy_where = array(
            "grade_id" => array(
                "gt",1
            )
        );
        $m_buy = $this->memberModel->getMemberCount($m_buy_where);
        $data['m_buy'] = $m_buy;//分销商总数
        $data['mall'] = $mall;//用户总数
        
        $data['order_yesterday'] = $this->_getOrderMoney($time1,$time2);//昨天的订单金额
        $data['order_yesterday_num'] = $this->_getOrderC($time1,$time2);//昨天的订单数
        
        //今天订单总额
        $data['order_today'] = $this->_getOrderMoney($today1,$today2);//今天的订单金额
        $data['order_today_num'] = $this->_getOrderC($today1,$today2);//今天的订单数
        
        //本周金额
        $data['order_week'] = $this->_getOrderMoney($week1,$week2);//本周的订单金额
        $data['order_week_num'] = $this->_getOrderC($week1,$week2);//本周的订单数
        
        //上周
        $data['order_lastweek'] = $this->_getOrderMoney($week3,$week4);//上周的订单金额
        $data['order_lastweek_num'] = $this->_getOrderC($week3,$week4);//上周的订单数
        
        //本月
        $data['order_month'] = $this->_getOrderMoney($month1,$month2);//本月的订单金额
        $data['order_month_num'] = $this->_getOrderC($month1,$month2);//本月的订单数
        
        //下月
        $data['order_lastmonth'] = $this->_getOrderMoney($month3,$month4);//上月的订单金额
        $data['order_lastmonth_num'] = $this->_getOrderC($month3,$month4);//上月的订单数
        
        //总的
        $data['order_all'] = $this->_getOrderMoney('2015-12-01 00:00:00',$today2);//所有的订单金额
        $data['order_all_num'] = $this->_getOrderC('2015-12-01 00:00:00',$today2);//所有的的订单数
        
        
        //0待支付,1已付款,,2完成,3.待退款 4已退款,5发货,0未完成,6订单关闭,
        $data['order_daifahuo'] = $this->_getOrderCount('2015-12-01 00:00:00',$today2,1);//待发货订单
        
        $data['order_tuihuo'] = $this->_getOrderCount('2015-12-01 00:00:00',$today2,3);//待退款订单
        
        $data['order_nopay'] = $this->_getOrderCount('2015-12-01 00:00:00',$today2,0);//待付款订单
        
        //已经支出的总佣金
        $where_w1 = array(
            "status" => array("IN","1")
        );
        $withdraw_moneyed = $this->withdrawModel->where($where_w1)->sum("atm_money");
        $data['withdraw_moneyed'] = $withdraw_moneyed ? $withdraw_moneyed : 0;
        
        
        $where_w2 = array(
            "status" => array("IN","0")
        );
        $withdraw_moneyimg = $this->withdrawModel->where($where_w2)->sum("atm_money");
        $data['withdraw_moneyimg'] = $withdraw_moneyimg ? $withdraw_moneyimg : 0;
        $this->assign('data',$data);
        
        
        //统计短信数量
        $smsNum = C('SMS_NUM');
        $useNum = M('smscode')->where(array('send_status'=>1))->count();
        $leftNum = $smsNum-$useNum;
        $this->assign('useNum',$useNum);
        $this->assign('leftNum',$leftNum);
        
        $this->display("index");
    }
    
    
    
    
    
    
    
}